"""
Case Type   : python驱动psycopg2
Case Name   : 连接参数异常时，sha256方式连接数据库
Create At   : 2024/3/28
Owner       : @ningyali
Description :
    1.解压openGauss-*-CentOS-x86_64-Python.tar.gz得到lib,替换psycopg2
    2.配置白名单
    3.配置lib
    4.数据库名错误，连接数据库
    5.用户名错误，连接数据库
    6.密码错误，连接数据库
    7.主机host错误，连接数据库
    8.端口号错误，连接数据库
Expect      :
    1.拷贝成功
    2.配置成功
    3.配置成功
    4.连接数据库失败
    5.连接数据库失败
    6.连接数据库失败
    7.连接数据库失败
    8.连接数据库失败
History     :
"""
import os
import unittest

from testcase.CONNECTORS.PDBC.PSYCOPG2. \
    Opengauss_Function_Connect_Psycopg_Common import Psycopg2Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class Psycopg34(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        text = f'-----{os.path.basename(__file__)} start-----'
        self.log.info(text)
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.conn_info = {'dbname': self.pri_sh.node.db_name,
                          'user': self.pri_sh.node.db_user,
                          'password': self.pri_sh.node.db_password,
                          'host': self.pri_sh.node.ssh_host,
                          'port': self.pri_sh.node.db_port}
        self.psycopg_com = Psycopg2Common(**self.conn_info)

        text = '----前置操作: 备份白名单 expect: 备份成功----'
        self.log.info(text)
        res = self.psycopg_com.bak_conf(bak_flag=True)
        self.log.info(res)
        self.assertTrue(res, f'执行失败: {text}')

    def test_1(self):
        text = '----step1: 解压PDBC得到lib,替换psycopg2 expect: 执行成功----'
        res = self.psycopg_com.decompress_pkg()
        self.assertTrue(res, f'执行失败: {text}')
        res = self.psycopg_com.set_new_psycopg2()
        self.assertTrue(res, f'执行失败: {text}')

        text = '----step2: 配置白名单 expect: 配置成功----'
        self.log.info(text)
        res = self.psycopg_com.set_pghba(open_flag=True)
        self.assertTrue(res, f'执行失败: {text}')

        text = '----step3: 配置lib expect: 成功----'
        self.log.info(text)
        res = self.psycopg_com.set_lib_path()
        self.assertTrue(res, f'执行失败: {text}')

        text = '----step4: 数据库名错误，连接数据库 expect: 连接失败----'
        self.log.info(text)
        tmp_val = self.psycopg_com.dbname
        self.psycopg_com.dbname = 'err_db'
        res = self.psycopg_com.conn_db()
        self.psycopg_com.dbname = tmp_val
        expect = 'psycopg2.OperationalError: FATAL:  ' \
                 'database "err_db" does not exist'
        self.assertIn(expect, res, f'执行失败: {text}')

        text = '----step5: 用户名错误，连接数据库 expect: 连接失败----'
        self.log.info(text)
        tmp_val = self.psycopg_com.user
        self.psycopg_com.user = 'err_user'
        res = self.psycopg_com.conn_db()
        self.psycopg_com.user = tmp_val
        expect = 'psycopg2.OperationalError: FATAL:  ' \
                 'Invalid username/password,login denied.'
        self.assertIn(expect, res, f'执行失败: {text}')

        text = '----step6: 密码错误，连接数据库 expect: 连接失败----'
        self.log.info(text)
        tmp_val = self.psycopg_com.password
        self.psycopg_com.password = 'err_password'
        res = self.psycopg_com.conn_db()
        self.psycopg_com.password = tmp_val
        self.assertIn(expect, res, f'执行失败: {text}')

        text = '----step7: 主机host错误，连接数据库 expect: 连接失败----'
        self.log.info(text)
        tmp_val = self.psycopg_com.host
        self.psycopg_com.host = '10.10.10.10'
        res = self.psycopg_com.conn_db()
        self.psycopg_com.host = tmp_val
        expect = 'psycopg2.OperationalError: could not connect to server: ' \
                 'Operation now in progress'
        self.assertIn(expect, res, f'执行失败: {text}')

        text = '----step8: 端口号错误，连接数据库 expect: 连接失败----'
        self.log.info(text)
        tmp_val = self.psycopg_com.port
        self.psycopg_com.port = str(int(tmp_val) + 1)
        res = self.psycopg_com.conn_db()
        self.psycopg_com.port = tmp_val
        expect = 'psycopg2.OperationalError: FATAL:  the local listen ip ' \
                 'and port is not for the gsql client'
        self.assertIn(expect, res, f'执行失败: {text}')

    def tearDown(self):
        text = '----run teardown----'
        self.log.info(text)

        text1 = f'----后置操作：删除解压文件目录----'
        self.log.info(text1)
        res1 = self.psycopg_com.clean_decompress_file()

        text2 = f'----后置操作：还原白名单----'
        self.log.info(text2)
        res2 = self.psycopg_com.bak_conf(bak_flag=False)

        self.assertTrue(res1, f'执行失败: {text1}')
        self.assertTrue(res2, f'执行失败: {text2}')

        text = f'-----{os.path.basename(__file__)} end-----'
        self.log.info(text)
